package org.monster.boot;

/**
 * dos命令验证
 * 提供对dos命令合法性的验证。
 *
 * @author Monster
 */
public class DosValidate {

    /**
     * 验证
     * 验证Dos命令是否正确
     *
     * @param dosCommand {@link String}
     * @return {@link Boolean}
     */
    public String validate(String dosCommand) {
        int startSym = dosCommand.indexOf('{');
        int endSym = dosCommand.lastIndexOf('}');
        if (startSym != -1 && endSym != -1) {
            dosCommand = validateDivsion(dosCommand);
            dosCommand = validateLine(dosCommand);
            String[] dosCommands = validateSpace(dosCommand);
            if (dosCommands != null && dosCommands.length == 3)
                return dosCommand;
        }
        return null;
    }

    /**
     * 验证空格
     * 验证Dos命令空格
     *
     * @param dosCommand dos命令
     * @return {@link String[]} dos命令字符串数组
     */
    private String[] validateSpace(String dosCommand) {
        String[] str = null;
        if (dosCommand.contains(Constants.STR_A))
            str = dosCommand.split(Constants.STR_A);
        return str;
    }

    /**
     * 验证Dos命令格式行
     *
     * @param dosCommand {@link String}
     * @return {@link String}
     */
    private String validateLine(String dosCommand) {
        if (dosCommand.contains(Constants.STR_B)) {
            String[] split = dosCommand.split(Constants.STR_B);
            dosCommand = split(dosCommand, split);
        }
        return dosCommand;
    }

    /**
     * 验证Dos命令二行
     *
     * @param dosCommand {@link String}
     * @return {@link String}
     */
    private String validateDivsion(String dosCommand) {
        if (dosCommand.contains(Constants.STR_C)) {
            String[] split = dosCommand.split(Constants.STR_C);
            dosCommand = split(dosCommand, split);
        }
        return dosCommand;
    }


    /**
     * 分割
     *
     * @param dosCommand dos命令
     * @param split      分割字符串数组
     * @return dos命令
     */
    private String split(String dosCommand, String[] split) {
        if (split[0] != null) {
            dosCommand = split[0];
        }
        return dosCommand;
    }
}
